<template>
    <div>
        <basic-container>
            <avue-form ref="form" v-model="formData" :option="formOption" @submit="submit">
                <template slot-scope="scope" slot="settlementImg">
                    <div>
                        <el-upload
                                class="avatar-uploader"
                                :action="'/pds/f/pds/api/common/goods/uploadPhoto'"
                                :show-file-list="false"
                                :data="uploadParams"
                                :on-success="uploadPhotoSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="formData.settlementImg" :src="formData.settlementImg" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">上传图片只能是jpeg/gif/jpg/png/bmp格式!
                            </div>
                        </el-upload>
                    </div>
                </template>
            </avue-form>
        </basic-container>
    </div>
</template>

<script>
    import {
        formOption
    } from '@/const/crud/scm/finance/settlement/settlement_edit'
    import {
        completeSettlement
    } from '@/api/scm/finance/settlement_record'
    import {getStore, setStore} from "@/util/store";

    export default {
        data() {
            return {
                formData: {
                    pmId: '',
                    settlementId: '',
                    settlementType: '',
                    settlementAmount: '',
                    payType: '',
                    payDate: '',
                    companyId: '',
                    companyName: '',
                    companyOperator: '',
                    settlementImg: '',
                    settlementDesc: '',
                    paymentSn: '',
                    operator: '',
                    status: ''
                },
                formOption: formOption,
                uploadParams:{
                    shopId: '',
                    pmId: '',
                }
            }
        },
        created() {
            this.formData = this.$route.query;
            this.pmId = this.getStore({name: "pmId"});
            this.uploadParams.pmId = this.pmId;
            this.uploadParams.shopId = getStore({name: "shopId"});

        },
        methods: {
            submit() {
                if(!this.formData.settlementImg){
                    this.$message.warning("须上传结算凭证图片");
                    return;
                }
                completeSettlement({...this.formData}).then(res=>{
                    if(res.data.code === 0){
                        this.$message.success("结算成功");
                        this.$router.push({path: '/funds/settlement'});
                    }else{
                        this.$message.warning(res.data.msg);
                    }
                });

            },
            uploadPhotoSuccess(res) {
                this.formData.settlementImg = res.ext.photoUrl
            },
            // 图片上传前验证
            beforeAvatarUpload(file){
                let isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!');
                }
                return isLt2M;
            }
        }
    }
</script>

<style lang="scss" scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
